<template>
  <el-dialog model-value="dialogVisible" :title="dialogTitle" width="30%" @close="handleClose" class="el-dialog">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">

      <el-form-item label="工作室" prop="studioId">
        <el-select v-model="form.studioId" class="widthForm" placeholder="请选择工作室">
          <el-option v-for="item in selectStudioList" :key="item.id" :label="item.studioName" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="团队名称" prop="commiteName">
        <el-input v-model="form.teamName" size="large" class="widthForm" />
      </el-form-item>
      <el-form-item label="团队描述" prop="commiteName">
        <el-input v-model="form.teamDes" type="textarea" size="large" class="widthForm" />
      </el-form-item>
      <el-form-item label="团队封面" prop="teamCover">
        <el-upload class="avatar-uploader" :action="getServerUrl() + 'sys/user/uploadImage'" :show-file-list="false"
          :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>

      </el-form-item>


    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, defineProps, ref, watch } from "vue";
import requestUtil, { getServerUrl } from "@/utils/request";
import { ElMessage } from "element-plus";
import { UploadProps } from 'element-plus'
import { Plus } from "@element-plus/icons-vue";

const tableData = ref([]);

const props = defineProps({
  id: {
    type: Number,
    default: -1,
    required: true,
  },
  dialogTitle: {
    type: String,
    default: "",
    required: true,
  },
  dialogVisible: {
    type: Boolean,
    default: false,
    required: true,
  },
});


const form = ref({
  id: -1,
  studioId: "",
  teamCover: "",
  teamDes: "",
  teamName: "",
});

const imageUrl = ref('')




const formRef = ref(null);

const initFormData = async (id) => {
  let data = { id };
  const res = await requestUtil.get("team/getTeamById", data);

  form.value = res.data.team;

  imageUrl.value = getServerUrl() + "images/userAvatar/" + form.value.teamCover;
};


watch(
  () => props.dialogVisible,
  () => {
    let id = props.id;
    if (id != -1) {
      initFormData(id);
      selectStudioListFun()
    } else {
      imageUrl.value = false
      selectStudioListFun()
      form.value = {
        id: -1,
        teamId: "",
        teamCover: "",
        teamDes: "",
        teamName: "",
      };
    }

  }
);



const handleAvatarSuccess = (res) => {
  console.log(res);
  imageUrl.value = getServerUrl() + res.data.src;
  form.value.teamCover = res.data.title;
};

const beforeAvatarUpload = (file) => {
  const isJPG = file.type === "image/jpeg";
  const isLt2M = file.size / 1024 / 1024 < 2;

  if (!isJPG) {
    ElMessage.error("图片必须是jpg格式");
  }
  if (!isLt2M) {
    ElMessage.error("图片大小不能超过2M!");
  }
  return isJPG && isLt2M;
};



// 工作室下拉框
const selectStudioList = ref([]);
const selectStudioListFun = async () => {
  let data = {
    pageNum: 1,
    pageSize: 100,
    studioName: ""
  }
  const res = await requestUtil.post("studio/getStudioList", data);
  selectStudioList.value = res.data.studioList.records;
};


const rules = ref({
  teamId: [
    { required: true, message: "请选择工作室", trigger: "blur" },
    { required: true, trigger: "blur" },
  ],

  teamCover: [
    { required: true, message: "请上传", trigger: "blur" }
  ],
  teamDes: [
    { required: true, message: "请输入", trigger: "blur" }
  ],
  teamName: [
    { required: true, message: "请输入", trigger: "blur" }
  ],
});


const emits = defineEmits(["update:modelValue", "initUserList"]);

const handleClose = () => {
  emits("update:modelValue", false);
};

const handleConfirm = async () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let result;
      if (form.value.id != -1) {
        result = await requestUtil.post("team/updateTeam", form.value);
      } else {
        result = await requestUtil.post("team/saveTeam", form.value);
      }

      let data = result.data;
      if (data.code == 200) {
        ElMessage.success(data.msg);
        formRef.value.resetFields();
        emits("initUserList");
        handleClose();
      } else {
        ElMessage.error(data.msg);
      }
    } else {
      console.log("fail");
    }
  });
};
</script>

<style scoped>
.widthForm {
  width: 100%;
}

.el-dialog {
  padding: 0 40px;
}

.demo-date-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-date-picker .block:last-child {
  border-right: none;
}

.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
